import request from '@/utils/request.js'
import { toast } from '@/utils/tool.js'
export function getOpenId() {
	return new Promise(resolve => {
		uni.login({
			provider: 'weixin',
			success: async ev => {
				const { code } = ev
				const resp = await request({
					url: "/login/mnpLogin",
					data: { code },
				})
				if (resp.code !== 1) {
					return resolve([resp, null])
				}
				const { openid } = resp.data
				resolve([null, openid])
			},
			fail: err => resolve([err, null])
		})
	})
}

export function mpWxPay(opt, id, { success, fail }) {
	const { config } = opt
	uni.requestPayment({
		...config,
		success: () => {
			success && success()
		},
		fail: () => {
			fail && fail(id)
		}
	})
}

export async function wxPerPay(order_id, { fail, success }) {
	// const [loginErr, open_id] = await getOpenId()
	// if (loginErr) {
	// 	return fail && fail()
	// }
	const payData = await request({
		url: "/pay/prepay",
		data: {
			from: 'order',
			pay_way: 2,
			order_id,
			open_id: '',
		},
	})
	const { code: payCdoe, data: payOption, msg } = payData
	if (payCdoe !== 1) {
		return fail && fail()
	}
	success && success(payOption, order_id)
}